/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author A7med Kotb
 */
public class LiveServersBean extends org.apache.struts.action.ActionForm 
{
    private String ip="";
    private String type="";
    private String userName="";
    private String password="";
    private String port="";
    private String description="";
    private String schedule="";
    private String modifier="";
    private String startDate="";
    private Date startDated;
    private String startTime="";
    private String[] day;
    private String index="";
    private static final String IPADDRESS_PATTERN ="^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
    //private static final String portnumber_pattern="^(6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{0,3})";
    private Pattern pattern;
    private Matcher matcher;
    //private Pattern pattern1;
    //private Matcher matcher1;
    private String[] month;

    public String[] getMonth() {
        return month;
    }

    public void setMonth(String[] month) {
        this.month = month;
    }

    public Date getStartDated() 
    {
        Date date=null;
        try 
        {  
            String str_date=getStartDate();
            DateFormat formatter ; 
            formatter = new SimpleDateFormat("MM/dd/yyyy");
            java.util.Date d = (java.util.Date)formatter.parse(str_date);
            date = new Date(d.getTime());
        } 
        catch (ParseException e)
        {
           e.printStackTrace();
        }  
        return date;
    }

    public void setStartDated(Date startDated) {
        this.startDated = startDated;
    }
    
    
    
    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    
    public String[] getDay() {
        return day;
    }

    public void setDay(String[] day) {
        this.day = day;
    }

    public String getModifier() {
        return modifier;
    }

    public void setModifier(String modifier) {
        this.modifier = modifier;
    }

    public String getSchedule() {
        return schedule;
    }

    public void setSchedule(String schedule) {
        this.schedule = schedule;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }
    

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    /**
     *
     */
    public LiveServersBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if(getIndex().equalsIgnoreCase("newliveserver"))
        {
            try {
                pattern = Pattern.compile(IPADDRESS_PATTERN);
                boolean result=validateIP(getIp());
                if(getIp()== null || getIp().length()<1)
                {
                    errors.add("EmptyLiveServerIp", new ActionMessage("error.liveserverip"));
                }
                else
                {
                    if(result==false)
                    {
                        errors.add("EmptyLiveServerIp", new ActionMessage("error.liveserverip"));
                    }
                }
                
                if(getType().equals("0"))
                {
                    errors.add("EmptyLiveServerType", new ActionMessage("error.liveservertype"));
                }
                if(getUserName()==null || getUserName().length()<1)
                {
                   errors.add("EmptyLiveServerUserName", new ActionMessage("error.liveserverusername")); 
                }
                if(getPort()== null || getPort().length()<1)
                {
                    errors.add("EmptyLiveServerPortNumber", new ActionMessage("error.liveserverportnumber")); 
                }
                else
                {
                   // pattern1 = Pattern.compile(IPADDRESS_PATTERN);
                    //boolean result1=validatePort(getPort());
                    try
                    {
                        int portNumber=Integer.parseInt(getPort());
                            if(portNumber<0 || portNumber>65535)
                            {
                                errors.add("EmptyLiveServerPortNumber", new ActionMessage("error.liveserverportnumber"));
                            }
                    }
                    catch(NumberFormatException ex)
                    {
                        errors.add("EmptyLiveServerPortNumber", new ActionMessage("error.liveserverportnumber"));
                    }
                }
                if(getSchedule().equals("0"))
                {
                    errors.add("EmptySchedule", new ActionMessage("error.schedule"));
                }
                if(getStartDate()== null || getStartDate().length()<1)
                {
                    errors.add("EmptyStartDate", new ActionMessage("error.startdate"));
                }
                else
                {
                    Calendar cal = Calendar.getInstance();
                    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
                    String dateNow = formatter.format(cal.getTime());
                    java.util.Date dn = formatter.parse(dateNow);
                    java.util.Date du=new java.util.Date(getStartDated().getTime());
                    if( !du.equals(dn))
                    {
                        if(du.before(dn))
                        {
                            errors.add("EmptyStartDate", new ActionMessage("error.startdate")); 
                        }
                     }
                 }
                if(getStartTime()==null || getStartTime().length()<1)
                {
                      errors.add("EmptyStartTime", new ActionMessage("error.starttime"));
                }
            } catch (ParseException ex) {
                Logger.getLogger(LiveServersBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
        return errors;
    }
     public boolean validateIP(final String ip){		  
	  matcher = pattern.matcher(ip);
	  return matcher.matches();	    	    
    }
//     public boolean validatePort(final String port){		  
//	  matcher1 = pattern1.matcher(ip);
//	  return matcher1.matches();	    	    
//    }
}
